// src/store/index.js
import { reactive } from 'vue'

const store = {
    state: reactive({
        user: null,
        todos: JSON.parse(localStorage.getItem('todos')) || [],
        filter: 'all' // 添加过滤状态
    }),
    
    setUser(user) {
        this.state.user = user
    },
    
    logout() {
        this.state.user = null
    },
    
    addTodo(todo) {
        // 修改待办事项结构，添加completed属性
        this.state.todos.push({
            text: todo,
            completed: false
        })
        this.saveTodos()
    },
    
    updateTodo(index, updatedTodo) {
        this.state.todos[index].text = updatedTodo
        this.saveTodos()
    },
    
    deleteTodo(index) {
        this.state.todos.splice(index, 1)
        this.saveTodos()
    },
    
    // 添加切换完成状态的方法
    toggleTodo(index) {
        this.state.todos[index].completed = !this.state.todos[index].completed
        this.saveTodos()
    },
    
    // 设置过滤器
    setFilter(filter) {
        this.state.filter = filter
    },
    
    // 获取过滤后的待办事项
    getFilteredTodos() {
        switch (this.state.filter) {
            case 'active':
                return this.state.todos.filter(todo => !todo.completed)
            case 'completed':
                return this.state.todos.filter(todo => todo.completed)
            default:
                return this.state.todos
        }
    },
    
    saveTodos() {
        localStorage.setItem('todos', JSON.stringify(this.state.todos))
    }
}

export default store